package com.lidroid.xutils.sample.menuapp.test;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.sample.R;
import com.lidroid.xutils.sample.R.layout;
import com.lidroid.xutils.sample.menuapp.adaptor.MenusAdaptor;
import com.lidroid.xutils.sample.menuapp.enty.Dish;
import com.lidroid.xutils.sample.menuapp.enty.Step;
import com.lidroid.xutils.view.annotation.ViewInject;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class testDishListActivity extends Activity {
	   String dataStr="";

	@ViewInject(R.id.menulist_lv) private ListView menusList_Lv;
	@ViewInject(R.id.result)private TextView resultText;
	 private Context mAppContext;
	 private MenusAdaptor menusAdaptor;
	 private ArrayList<Dish> menuDataList=new ArrayList<Dish>();
 
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_menu_list);
		System.out.println("sdkfsygkfdjfcxvcxnb");
		ViewUtils.inject(this);
		mAppContext=this.getApplicationContext();
//		menuDataList=new ArrayList<Dish>();
 
//		
		for (int i = 0; i < 5; i++) {
			Dish dish =new Dish();
			dish.setName("title"+i);
			dish.setTags("标签"+i);
			menuDataList.add(dish);
		}
		
		
//		
//		ClientsAPI.testGet();
//		 System.out.println("调用之前"+dataStr);
//		menuDataList= ClientsAPI.getWangQuData(dataStr);
		menusAdaptor=new MenusAdaptor(mAppContext);
		System.out.println(" wq  menudatalist"+menuDataList.size());
		menusAdaptor.BindData(menuDataList);
		menusList_Lv.setAdapter(menusAdaptor);
		menusAdaptor.notifyDataSetChanged();
		
		testGet();
	 
	}
	
	
	  public void testGet() {

	        //RequestParams params = new RequestParams();
	        //params.addHeader("name", "value");
	        //params.addQueryStringParameter("name", "value");
	    	
	    	
	        RequestParams params = new RequestParams();
	      //  params.addHeader("name", "value");
	        params.addQueryStringParameter("key", "32ed6d60c15521d401097c96f834328c");
	        params.addQueryStringParameter("dtype", "json");
	        params.addQueryStringParameter("menu", "西红柿");
	        HttpUtils http = new HttpUtils();
	        http.configCurrentHttpCacheExpiry(1000 * 10);
	        http.send(HttpRequest.HttpMethod.GET,
	                "http://apis.juhe.cn/cook/query.php",
	                params,
	                new RequestCallBack<String>() {

	                    @Override
	                    public void onStart() {
	                        resultText.setText("conn...");
	                    }

	                    @Override
	                    public void onLoading(long total, long current, boolean isUploading) {
	                        resultText.setText(current + "/" + total);
	                    }

	                    @Override
	                    public void onSuccess(ResponseInfo<String> responseInfo) {
	                 //       resultText.setText("response:" + responseInfo.result);
	                        

	                    	
	                    	String s = ""; 
	            
	                      	try { 
	                      		 JSONObject dataOfObject = new JSONObject(responseInfo.result);
	                      	   //获取键对应的值
	                      	   String firstName = dataOfObject.getString("resultcode");
	                      	   String lastName = dataOfObject.getString("reason");
	                      		
	                      	 JSONObject resultObj = dataOfObject.getJSONObject("result");
	                            JSONArray dataArr = resultObj.getJSONArray("data"); 
	        Dish dish=new Dish();
	                            for(int i = 0; i < dataArr.length() ; i++){ 
	                                JSONObject dataObj = dataArr.getJSONObject(i);
	                               
	                                int id = dataObj.getInt("id"); 
	                                String name = dataObj.getString("title"); 
	                                dish.setName(name);
	                                String gender = dataObj.getString("tags"); 
	                               dish.setTags( dataObj.getString("tags"));
	                               menuDataList.add(dish);

	                                JSONArray stepArr = dataObj.getJSONArray("steps"); 
	                                
	                                String step="";
	                                String img="";
	                                for (int j = 0; j < stepArr.length(); j++) {
	                                	JSONObject stepObj = stepArr.getJSONObject(j);
	                                	img=stepObj.getString("img"); 
	                                	step+= stepObj.getString("step"); 
									}
	                                s += firstName+lastName+ "ID号"+id + ", 姓名：" + name + ",性别：" + gender+ "steps:"+step+"\n" ;
	                                

	                            } 
	                     
	                        } catch (JSONException e) { 
	                            System.out.println("Jsons parse error !"); 
	                            e.printStackTrace(); 
	                        }
	               //     	resultText.setText(s); 
	                    }


	                    @Override
	                    public void onFailure(HttpException error, String msg) {
	                        resultText.setText(msg);
	                    }
	                });
	        
	    }
	
	
	
	 
	    
}
